import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;




public class Foriegnagents implements ActionListener
{
	DataOutputStream output;

	BufferedInputStream bis;
	
	BufferedOutputStream bos;

	byte[] receivedData;
	int in;
	
	String strLine;
	public Font f = new Font("Times new roman" , Font.BOLD , 16);	
	public Font f1 = new Font("Times new roman", Font.BOLD , 28);
	public Font f2 = new Font("Times new roman", Font.BOLD , 18);
	public Font f3 = new Font("Engravers MT", Font.BOLD , 15);
    public JLabel T1= new JLabel("Foriegn Agents");
	public JLabel T2= new JLabel();
	public JLabel l = new JLabel("Received File");

	public JLabel l1 = new JLabel("Foriegn Agent A");
	public JLabel l2 = new JLabel("Foriegn Agent B ");
	public JLabel l3= new JLabel("Foriegn Agent C");
	public JLabel l4 = new JLabel("Foriegn Agent D");
	
	
	public JLabel l5= new JLabel(" Received File");
	public JLabel l6= new JLabel("Received File");
	public JLabel l6a= new JLabel("Received File");
	
	
	
	public JButton btn = new JButton("Submit");
	public JTextArea tf = new JTextArea();
	public JScrollPane pane = new JScrollPane();
	
	
	
	public JTextArea tf1 = new JTextArea();
	public JScrollPane pane1 = new JScrollPane();
	public JTextArea tf2 = new JTextArea();
	public JScrollPane pane2 = new JScrollPane();
	public JTextArea tf3 = new JTextArea();
	public JScrollPane pane3 = new JScrollPane();
	
	public JFrame jf;
	public Container c;
	JPanel jp=new JPanel();
	JPanel jp1=new JPanel();
	
	JPanel jp2=new JPanel();
	JPanel jp3=new JPanel();
	JPanel jp4=new JPanel();
	JPanel jp5=new JPanel();
	
	JPanel jp6=new JPanel();
	JPanel jp7=new JPanel();
	JPanel jp8=new JPanel();
	JPanel jp9=new JPanel();
	
	Foriegnagents()
	{
		
				
		jf = new JFrame("Foriegn Agents");
		c = jf.getContentPane();
		c.setLayout(null);
		jf.setSize(1080,1080);
		
		c.setBackground(new Color(6,16,61));
		
		
		T1.setBounds(400,5,950,50);
		T2.setBounds(210,50,950,50);
		T1.setForeground(Color.MAGENTA);
		T2.setForeground(Color.RED);
		
		
		
		l.setBounds(700,395,250,35);
		
		l1.setBounds(190,65,150,35);
		l2.setBounds(700,65,150,35);
		l3.setBounds(190,365,250,35);
		l4.setBounds(700,365,150,35);
		
		
		l5.setBounds(190,90,150,35);
		l6.setBounds(700,90,250,35);
		l6a.setBounds(190,395,250,35);
		
		
		
		
		l.setForeground(new Color(120,0,0));
		//l.setForeground(new Color(120,0,0));
		l1.setForeground(Color.BLUE);
		l2.setForeground(Color.BLUE);
		l3.setForeground(Color.BLUE);
		
		l4.setForeground(Color.BLUE);
		l5.setForeground(new Color(120,0,0));
		l6.setForeground(new Color(120,0,0));
		l6a.setForeground(new Color(120,0,0));
		
		
		
		
		l.setFont(f2);
		l1.setFont(f2);
		l2.setFont(f2);
		l3.setFont(f2);
		l4.setFont(f2);
		l5.setFont(f2);
		l6.setFont(f2);
		l6a.setFont(f2);
		
		
		//tf.setBackground(new Color(151,242,241));
		tf.setForeground(Color.BLUE);
		tf1.setForeground(new Color(69,39,4));
		tf2.setForeground(new Color(170,5,5));
		tf3.setForeground(Color.MAGENTA);
		
		tf.setFont(f);
		tf1.setFont(f);
		tf2.setFont(f);
		tf3.setFont(f);
		
		jp.setBounds(40,60,960,610);
		jp.setBackground(new Color(245,202,122));
		jp1.setBounds(35,55,970,620);
		
		jp2.setBounds(86,95,390,250);
		jp2.setBackground(new Color(245,202,122));
		jp3.setBounds(85,94,392,252);
		jp3.setBackground(Color.BLACK);
		
		jp4.setBounds(556,95,390,250);
		jp4.setBackground(new Color(245,202,122));
		jp5.setBounds(555,94,392,252);
		jp5.setBackground(Color.BLACK);
		
		jp6.setBounds(86,396,390,250);
		jp6.setBackground(new Color(245,202,122));
		jp7.setBounds(85,395,392,252);
		jp7.setBackground(Color.BLACK);
		
		jp8.setBounds(556,396,390,250);
		jp8.setBackground(new Color(245,202,122));
		jp9.setBounds(555,395,392,252);
		jp9.setBackground(Color.BLACK);
		
		
		pane.setBackground(new Color(151,242,241));
		tf.setColumns(20);
		tf.setRows(10);
		tf.setName("tf");
		pane.setName("pane");
		pane.setViewportView(tf);
		pane.setBounds(104,125,350,200);
		
        pane1.setBounds(575,125,350,200);
                   
		tf1.setColumns(20);
		tf1.setRows(10);
		tf1.setName("tf");
		pane1.setName("pane");
		pane1.setViewportView(tf1);
				
		T1.setFont(f1);
		T2.setFont(f1);
			
        pane2.setBounds(100,430,350,200);
		
		tf2.setColumns(20);
		tf2.setRows(10);
		tf2.setName("tf");
		pane2.setName("pane");
		pane2.setViewportView(tf2);
		
       pane3.setBounds(575,430,350,200);
		tf3.setColumns(20);
		tf3.setRows(10);
		tf3.setName("tf");
		pane3.setName("pane");
		pane3.setViewportView(tf3);
		
		btn.setMnemonic(KeyEvent.VK_S); 
		jf.show();
		c.add(pane, BorderLayout.CENTER);
		c.add(pane1, BorderLayout.CENTER);
		c.add(pane2, BorderLayout.CENTER);
		c.add(pane3, BorderLayout.CENTER);
		c.add(T1);
		
		
		c.add(l);
		
		c.add(l1);
		c.add(l2);
		c.add(l3);
		c.add(l4);
		
		c.add(l5);
		c.add(l6);
		c.add(l6a);
		
		c.add(jp2);
		c.add(jp3);
		c.add(jp4);
		c.add(jp5);
		c.add(jp6);
		c.add(jp7);
		c.add(jp8);
		c.add(jp9);
		
		c.add(jp);
		c.add(jp1);
		
		
			
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent win) {
				System.exit(0);
			}
		});		
		
		int[] ports = new int[] { 8587,2222,3333,4444 };

		for (int i = 0; i < 4; i++) {
			Thread t = new Thread(new PortListener(ports[i]));
			t.setName("Listener-" + ports[i]);
			t.start();

		}
			
	}
	/******************************  end ******************************************** */	
	public static void main(String args[])
	{
		Foriegnagents s=new Foriegnagents();
				
	}
		 public void actionPerformed(ActionEvent e)
	 {
	 }
	class PortListener implements Runnable {
		
		BufferedOutputStream bos = null;
		
		ServerSocket server;
		Socket connection;
		ServerSocket server1;
		Socket connection1;
		BufferedReader br = null;
		int port;
         String sendstr;
         String sendstrb;
         String sendstrc;
         String sendstrd;
		public PortListener(int port) {
			this.port = port;
		}

		public void run() {
			if(this.port==8587)
			{
			
			try { 
				   
				    server = new ServerSocket(8587);
					
				     while (true)
				   {
					                   connection = server.accept();
					
					                   br = new BufferedReader(
							           new InputStreamReader(new BufferedInputStream(
									   connection.getInputStream())));
					
					                      String strLine;
					                     // Read File Line By Line
					                   StringBuffer buffer = new StringBuffer();
					                   System.out.println("hi");
					                   while ((strLine = br.readLine()) != null)
					                  {
						                  // Print the content on the console
						                   System.out.println(strLine);
						                   buffer.append(strLine + "\n");

					                   }
				                               	br.close();
					                            connection.close();
					                           
					                            tf.setText(buffer.toString());	
					                            String a="MobileTerminal,Foriegn Agent  A,Gateway FA  A,  MIP";
					    		                  int b=a.length();
					    		                  
					    		                  String aString = Integer.toString(b);
					    		                  sendstr=aString.toString();
					    		                  sendstr+=a;
					    		                  sendstr+=buffer.toString();
					                			
					/******************************************************************/
					/*******************************************************************/
					                       byte[] byteArray;			 
					                       Socket client = null;
					    try     { 
						
						                  client = new Socket("127.0.0.1", 8889);
			    		                  bos = new BufferedOutputStream(client.getOutputStream()); 
			    		                      
			    		                  
			    		                  byteArray = sendstr.getBytes();
			    		                  bos.write(byteArray, 0, byteArray.length);
			    		                  bos.flush();
			    		                  bos.close();
			    		                  client.close();
					           }
					              
					    catch (UnknownHostException e1) 
					          {
						        // TODO Auto-generated catch block
						           e1.printStackTrace();
					         }
					    catch (IOException e1) 
					         {
						       // TODO Auto-generated catch block
						          e1.printStackTrace();
					         }
					    
					    
					   
					    finally 
					           {
						            if (bos != null)
						            {
							                try 
							                  {
								              bos.close();
							                  }            
							                 catch (IOException e1)
							                   {
								                 // TODO Auto-generated catch block
								                 e1.printStackTrace();
							                    }
						           }
						 
						            try {
							                client.close();
					                   	} 
						            catch (IOException e1)
						                {
							             // TODO Auto-generated catch block
							               e1.printStackTrace();
						                }
					         }
					    
					    
					    
					    
					    
					    
				        }
			  }       
			           catch (IOException e)
			            {
			        	   
			             }   
			           finally
			           {
			        	   
			            }
			
			
		           }
		
			else if(this.port==2222)
			{

				
				try { 
					   
					    server1 = new ServerSocket(2222);
						
					     while (true)
					   {
						                   connection1 = server1.accept();
						
						                   br = new BufferedReader(
								           new InputStreamReader(new BufferedInputStream(
										   connection1.getInputStream())));
						
						                      String strLine;
						                     // Read File Line By Line
						                   StringBuffer buffer = new StringBuffer();
						                   System.out.println("hi");
						                   while ((strLine = br.readLine()) != null)
						                  {
							                  // Print the content on the console
							                   System.out.println(strLine);
							                   buffer.append(strLine + "\n");

						                   }
					                               	br.close();
						                            connection1.close();
						                           
						                            tf1.setText(buffer.toString());	
						                            String a1="MobileTerminal,Foriegn Agent  B,Gateway FA  A,DHMIP";
						    		                  int b1=a1.length();
						    		                  
						    		                  String bString = Integer.toString(b1);
						    		                  sendstrb=bString.toString();
						    		                  sendstrb+=a1;
						    		                  sendstrb+=buffer.toString();
						                			
						                           
						/******************************************************************/
						/*******************************************************************/
						                       byte[] byteArray;			 
						                       Socket client = null;
						    try     { 
							
							                  client = new Socket("127.0.0.1", 8889);
				    		                  bos = new BufferedOutputStream(client.getOutputStream()); 
				    		                 
				    		                  byteArray = sendstrb.getBytes();
				    		                  bos.write(byteArray, 0, byteArray.length);
				    		                  bos.flush();
				    		                  bos.close();
				    		                  client.close();
						           }
						              
						    catch (UnknownHostException e1) 
						          {
							        // TODO Auto-generated catch block
							           e1.printStackTrace();
						         }
						    catch (IOException e1) 
						         {
							       // TODO Auto-generated catch block
							          e1.printStackTrace();
						         }
						    
						    
						   
						    finally 
						           {
							            if (bos != null)
							            {
								                try 
								                  {
									              bos.close();
								                  }            
								                 catch (IOException e1)
								                   {
									                 // TODO Auto-generated catch block
									                 e1.printStackTrace();
								                    }
							           }
							 
							            try {
								                client.close();
						                   	} 
							            catch (IOException e1)
							                {
								             // TODO Auto-generated catch block
								               e1.printStackTrace();
							                }
						         }
								
						
						
					        }
				  }       
				           catch (IOException e)
				            {
				        	   
				             }   
				           finally
				           {
				        	   
				            }
				
				
			           
				
			}
			else if(this.port==3333)
			{


				
				try { 
					   
					    server1 = new ServerSocket(3333);
						
					     while (true)
					   {
						                   connection1 = server1.accept();
						
						                   br = new BufferedReader(
								           new InputStreamReader(new BufferedInputStream(
										   connection1.getInputStream())));
						
						                      String strLine;
						                     // Read File Line By Line
						                   StringBuffer buffer = new StringBuffer();
						                   System.out.println("hi");
						                   while ((strLine = br.readLine()) != null)
						                  {
							                  // Print the content on the console
							                   System.out.println(strLine);
							                   buffer.append(strLine + "\n");

						                   }
					                               	br.close();
						                            connection1.close();
						                           
						                            tf2.setText(buffer.toString());	
						                           
						                            
						                            String a2="MobileTerminal,Foriegn Agent  C,Gateway FA  B,MHMIP";
						    		                  int b2=a2.length();
						    		                  
						    		                  String cString = Integer.toString(b2);
						    		                  sendstrc=cString.toString();
						    		                  sendstrc+=a2;
						    		                  sendstrc+=buffer.toString();
						                			
						/******************************************************************/
						/*******************************************************************/
						                       byte[] byteArray;			 
						                       Socket client = null;
						    try     { 
							
							                  client = new Socket("127.0.0.1", 9999);
				    		                  bos = new BufferedOutputStream(client.getOutputStream()); 
				    		                  
				    		                  byteArray = sendstrc.getBytes();
				    		                  bos.write(byteArray, 0, byteArray.length);
				    		                  bos.flush();
				    		                  bos.close();
				    		                  client.close();
						           }
						              
						    catch (UnknownHostException e1) 
						          {
							        // TODO Auto-generated catch block
							           e1.printStackTrace();
						         }
						    catch (IOException e1) 
						         {
							       // TODO Auto-generated catch block
							          e1.printStackTrace();
						         }
						    
						    
						   
						    finally 
						           {
							            if (bos != null)
							            {
								                try 
								                  {
									              bos.close();
								                  }            
								                 catch (IOException e1)
								                   {
									                 // TODO Auto-generated catch block
									                 e1.printStackTrace();
								                    }
							           }
							 
							            try {
								                client.close();
						                   	} 
							            catch (IOException e1)
							                {
								             // TODO Auto-generated catch block
								               e1.printStackTrace();
							                }
						         }
								
						
						
					        }
				  }       
				           catch (IOException e)
				            {
				        	   
				             }   
				           finally
				           {
				        	   
				            }
				
				
			           
				
			
			}
		
			else if(this.port==4444)
			{



				
				try { 
					   
					    server1 = new ServerSocket(4444);
						
					     while (true)
					   {
						                   connection1 = server1.accept();
						
						                   br = new BufferedReader(
								           new InputStreamReader(new BufferedInputStream(
										   connection1.getInputStream())));
						
						                      String strLine;
						                     // Read File Line By Line
						                   StringBuffer buffer = new StringBuffer();
						                   System.out.println("hi");
						                   while ((strLine = br.readLine()) != null)
						                  {
							                  // Print the content on the console
							                   System.out.println(strLine);
							                   buffer.append(strLine + "\n");

						                   }
					                               	br.close();
						                            connection1.close();
						                           
						                            tf3.setText(buffer.toString());	
						                            
						                            
						                            String a3="MobileTerminal,Foriegn Agent  D,Gateway FA  B,MHMIP";
						    		                  int b3=a3.length();
						    		                  
						    		                  String dString = Integer.toString(b3);
						    		                  sendstrd=dString.toString();
						    		                  sendstrd+=a3;
						    		                  sendstrd+=buffer.toString();
						                           
						/******************************************************************/
						/*******************************************************************/
						                       byte[] byteArray;			 
						                       Socket client = null;
						    try     { 
							
							                  client = new Socket("127.0.0.1", 9999);
				    		                  bos = new BufferedOutputStream(client.getOutputStream()); 
				    		                  byteArray = sendstrd.getBytes();
				    		                  bos.write(byteArray, 0, byteArray.length);
				    		                  bos.flush();
				    		                  bos.close();
				    		                  client.close();
						           }
						              
						    catch (UnknownHostException e1) 
						          {
							        // TODO Auto-generated catch block
							           e1.printStackTrace();
						         }
						    catch (IOException e1) 
						         {
							       // TODO Auto-generated catch block
							          e1.printStackTrace();
						         }
						    
						    
						   
						    finally 
						           {
							            if (bos != null)
							            {
								                try 
								                  {
									              bos.close();
								                  }            
								                 catch (IOException e1)
								                   {
									                 // TODO Auto-generated catch block
									                 e1.printStackTrace();
								                    }
							           }
							 
							            try {
								                client.close();
						                   	} 
							            catch (IOException e1)
							                {
								             // TODO Auto-generated catch block
								               e1.printStackTrace();
							                }
						         }
								
						
						
					        }
				  }       
				           catch (IOException e)
				            {
				        	   
				             }   
				           finally
				           {
				        	   
				            }
				
				
			           
				
			
			
			}
		}
}
	
}
